package com.gjj.abox.login;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.gjj.abox.BaseActivity;
import com.gjj.abox.database.R;
import com.gjj.abox.utils.AnimalUtils;
import com.gjj.abox.utils.ToastUtil;

public class RegisterFirstActivity extends BaseActivity {

	private EditText etName, etPwd, etPwdAgain;
	private Button btnNext;
	private ImageView ivBack;
	private final String PREFERENCE_NAME = "source";
	private SharedPreferences sharedpreferences = null;
	private SharedPreferences.Editor edit = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_first);

		etName = (EditText) findViewById(R.id.et_register_name);
		etPwd = (EditText) findViewById(R.id.et_register_pwd);
		etPwdAgain = (EditText) findViewById(R.id.et_register_pwd_again);
		btnNext = (Button) findViewById(R.id.btn_register_first);
		ivBack = (ImageView) findViewById(R.id.iv_register_back);
		ivBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		sharedpreferences = getSharedPreferences(PREFERENCE_NAME,
				Activity.MODE_PRIVATE);
		btnNext.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				next();
			}

		});
	}

	private void next(){
		String userName = etName.getText().toString().trim();
		String userPwd1 = etPwd.getText().toString().trim();
		String userPwd2 = etPwdAgain.getText().toString().trim();
		if(userName.equals("")){
			AnimalUtils.shakeAnimal(etName);
		}else if(boxDb.checkUserAccountIsExist(userName)){
			ToastUtil.showShortToast(RegisterFirstActivity.this,"该用户名已存在！");
		} else if (userPwd1.equals("")) {
			AnimalUtils.shakeAnimal(etPwd);
		} else {
			if (userPwd1.equals(userPwd2)) {
				edit = sharedpreferences.edit();
				edit.putString("name", userName);
				edit.putString("pwd", userPwd1);
				edit.commit();
				Intent intent = new Intent(RegisterFirstActivity.this,
						RegisterSecondActivity.class);
				startActivity(intent);
				finish();
			} else {
				AnimalUtils.shakeAnimal(etPwdAgain);
				ToastUtil.showShortToast(RegisterFirstActivity.this,"两次密码不一致！");
			}
		}
	}
	@Override
	public void finish() {
		super.finish();
	}

}
